//
//  TransferAccountsViewController.m
//  TerritoryShopkeeper
//
//  Created by xiaowu on 2018/1/3.
//  Copyright © 2018年 ZDQK. All rights reserved.
//

#import "TransferAccountsViewController.h"
#import "StoresNearbyHomeModel.h"

@interface TransferAccountsViewController ()
@property (weak, nonatomic) IBOutlet UIView *headView;
@property (weak, nonatomic) IBOutlet UIImageView *headImageView;
@property (weak, nonatomic) IBOutlet UILabel *headLabel;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UIView *lineView;
@property (weak, nonatomic) IBOutlet UIButton *button;

@property (nonatomic, strong) StoresNearbyHomeModel *shopModel;
@end

@implementation TransferAccountsViewController
#pragma mark - IBAction
- (IBAction)TransferButtonAction:(id)sender {
    // 转账
    if (![self checkLogin]) {
        return;
    }
    
    if (!TextIsNotNil(self.textField.text)) {
        [MBProgressHUD myShowHUDAddedTo:self.view labelText:@"请输入转账积分数" animated:YES];
        return;
    }
    [self.view endEditing:YES];
    // 调用转账接口
    [self transferNetWork];
}
#pragma mark - UIViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self configureUI];
    [self getOtherUserInfo];
}
- (void)disMissSelf {
    [self popToRootViewController];
}
#pragma mark - 网络请求
- (void)getOtherUserInfo {
    [MBProgressHUD showHUDProgress:self.view animated:YES];
    __block typeof(self) blockSelf = self;
    [MYAFNetworking POST:URL_Get_Store_Info parameters:@{@"sid":self.sid} success:^(NSURLSessionDataTask * _Nullable task, id  _Nullable responseObject) {
        [MBProgressHUD hideHUDForView:blockSelf.view animated:YES];
        if ([responseObject[@"code"] isEqualToString:@"200"]) {
            blockSelf.shopModel = [StoresNearbyHomeModel mj_objectWithKeyValues:responseObject[@"data"]];
            [blockSelf uploadData];
        } else {
            [MBProgressHUD showError:blockSelf.view title:responseObject[@"message"] code:responseObject[@"code"] animated:YES];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nullable error) {
        [MBProgressHUD hideHUDForView:blockSelf.view animated:YES];
        [MBProgressHUD showError:blockSelf.view title:@"" code:@"" animated:YES];
    }];
}
- (void)transferNetWork {
    //MARK:测试 扫码支付
    NSMutableDictionary *parameter = [[NSMutableDictionary alloc] init];
    [parameter setObject:self.textField.text forKey:@"integral"];
    [parameter setObject:self.sid forKey:@"sid"];
    
    [MBProgressHUD showHUDProgress:self.view animated:YES];
    __block typeof(self) blockSelf = self;
    [MYAFNetworking POST:URL_Scan_Pay parameters:parameter success:^(NSURLSessionDataTask * _Nullable task, id  _Nullable responseObject) {
        [MBProgressHUD hideHUDForView:blockSelf.view animated:YES];
        if ([responseObject[@"code"] isEqualToString:@"200"]) {
            [MBProgressHUD myShowHUDAddedTo:blockSelf.view labelText:@"支付成功~" animated:YES];
            [blockSelf disMissSelf];
        } else {
            [MBProgressHUD showError:blockSelf.view title:responseObject[@"message"] code:responseObject[@"code"] animated:YES];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nullable error) {
        [MBProgressHUD hideHUDForView:blockSelf.view animated:YES];
        [MBProgressHUD showError:blockSelf.view title:@"" code:@"" animated:YES];
    }];
}
#pragma mark - Method
- (void)configureUI {
    self.navigationItem.title = @"转账";
    
    self.view.backgroundColor = [UIColor backgroundColor];
    self.headView.backgroundColor = [UIColor colorWithHex:0xFBFBFB alpha:1.0];
}
- (void)uploadData {
    [self.headImageView sd_setImageWithURL:GetUrl(self.shopModel.simg) placeholderImage:PlaceholderImageHeader];
    
    NSString *nameStr = [NSString stringWithFormat:@"向 %@ 转账",self.shopModel.sname];
    NSMutableAttributedString *nameAttStr = [[NSMutableAttributedString alloc] initWithString:nameStr];
    [nameAttStr setAttributes:@{NSForegroundColorAttributeName:[UIColor blackTextColor]} range:NSMakeRange(0, nameStr.length)];
    [nameAttStr setAttributes:@{NSForegroundColorAttributeName:[UIColor ThemeColor]} range:[nameStr rangeOfString:self.shopModel.sname]];
    
    [self.headLabel setAttributedText:nameAttStr];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    [self.view endEditing:YES];
}

@end
